<template>
  <Dialog v-model="dialogVisible" title="下发文件" width="550" @close="dialogVisible = false">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      class="form-wrap"
      label-width="120px"
    >
      <el-form-item label="标准类型" prop="type">
        <el-select
          v-model="formData.type"
          clearable
          placeholder="请选择标准类型"
          @change="changeType"
        >
          <el-option label="结构化数据" :value="1" />
          <el-option label="非结构化数据" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据标准" prop="template">
        <el-select
          v-model="formData.template"
          clearable
          placeholder="请选择数据标准"
          value-on-clear=""
        >
          <el-option
            v-for="item in standardList"
            :key="item.id"
            :label="item.dataStandardName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-bottom: 10px">
        <Icon icon="svg-icon:tips" />
        <span style="color: #cccccc">文件的归属部门及归属系统与父文件组一致</span>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as StandardsApi from '@/api/dataStandards/standards'
import type { FormInstance } from 'element-plus'
import { issuaFile } from '@/api/infra/file'

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中
const message = useMessage() // 消息弹窗
const open = (id) => {
  formData.value.fileId = id
  formData.value.type = ''
  formData.value.template = ''
  dialogVisible.value = true
}
const standardList = ref([])
const formData = ref({ fileId: '', type: '', template: '' })
const formRules = reactive({
  type: [{ required: true, message: '请选择标准类型', trigger: ['blur', 'change'] }],
  template: [{ required: true, message: '请选择数据标准', trigger: ['blur', 'change'] }]
})
const formRef = ref<FormInstance>() // 表单 Ref

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef.value) return
  const valid = await formRef.value?.validate()
  if (!valid) {
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    await issuaFile(formData.value)
    message.success('下发成功')
    closeDialog()
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
//根据标准类型获取数据标准
const changeType = async (val) => {
  getStandList(val)
}
//获取数据标准
const getStandList = async (type) => {
  const { data } = await StandardsApi.dataStandardsPage({
    page: 1,
    pageSize: 10000,
    status: '1',
    type
  })
  standardList.value = data
}

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

/** 重置表单 */

function closeDialog() {
  dialogVisible.value = false
}
</script>
<style lang="scss" scoped></style>
